//
//  CLPickerView.swift
//  HandNUPT
//
//  Created by Joggy on 15/12/4.
//  Copyright © 2015年 NUPT. All rights reserved.
//

import UIKit

private let screenWidth = UIScreen.main.bounds.width
private let screenHeight = UIScreen.main.bounds.height

class CLPickerView: UIView {
    
    let contentTitle: [String] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
    
    var picker: UIPickerView!
    var bgShadow: UIVisualEffectView!
    var selectBut: UIButton!
    var keyWindow: UIWindow!

    override init(frame: CGRect) {
        super.init(frame: frame)
        keyWindow = UIApplication.shared.keyWindow! as UIWindow
        backgroundColor = UIColor.clear
        self.frame.origin.y = screenHeight
        prepareForLayout()
    }
    
    func prepareForLayout() {
        picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
        picker.delegate = self
        picker.dataSource = self
        picker.showsSelectionIndicator = true
        addSubview(picker)
        selectBut = UIButton(type: UIButtonType.system)
        selectBut.setAttributedTitle(NSAttributedString(string: "选择", attributes: [NSForegroundColorAttributeName: UIColor(red: 0.29, green: 0.686, blue: 0.494, alpha: 1), NSFontAttributeName: UIFont.systemFont(ofSize: 18)]), for: UIControlState())
        selectBut.frame = CGRect(x: 0, y: 0, width: 66, height: 44)
        selectBut.sizeToFit()
        selectBut.layer.position = CGPoint(x: self.frame.width - selectBut.frame.width, y: selectBut.frame.height)
        addSubview(selectBut)
        bgShadow = UIVisualEffectView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        bgShadow.effect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
        bgShadow.alpha = 0
        let tapGesture = UITapGestureRecognizer()
        tapGesture.addTarget(self, action: #selector(CLPickerView.dismiss))
        bgShadow.addGestureRecognizer(tapGesture)
    }
    
    func show() {
        keyWindow.addSubview(bgShadow)
        keyWindow.addSubview(self)
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.bgShadow.alpha = 1
            self.frame.origin.y = screenHeight*2/3
        }) 
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.bgShadow.alpha = 0
            self.frame.origin.y = screenHeight
            }, completion: { (finish) -> Void in
                self.bgShadow.removeFromSuperview()
                self.removeFromSuperview()
        }) 
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CLPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return contentTitle.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return contentTitle[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return screenWidth
    }
    
}





